package com.fm.jbsql.util;

import org.xml.sax.helpers.DefaultHandler;
import java.util.Vector;
import java.util.HashMap;

import com.fm.jbsql.entry.Entry;

/**
 * Created by IntelliJ IDEA.
 * User: syaroshenko
 * Date: Jul 13, 2006
 * Time: 11:36:27 AM
 * Class to extend default XML-parser handler.
 */

/**
 * Class to extend default XML-parser handler.
 */
public final class SvnEntryPlainTextHandler extends DefaultHandler {
    private Vector entries;
    private final boolean isGetRevisionMode;
    private final String []inputFiles;

    /**
     * Constructor.
     * @param _entries vector of entries
     * @param _isGetRevisionMode retrieve only revision of provided objects in _inputFiles array
     * @param _inputFiles file names array to query revision
     * @throws JbsqlException
     */
    public SvnEntryPlainTextHandler (Vector _entries, boolean _isGetRevisionMode, String []_inputFiles) throws JbsqlException {
        super();

        if ( _entries == null ) throw new JbsqlException("NPE_01", null, null);
        if ( _isGetRevisionMode && (_inputFiles == null || _inputFiles.length == 0) ) throw new JbsqlException("JEX_01", null, null);
        entries = _entries;
        isGetRevisionMode = _isGetRevisionMode;
        inputFiles = _inputFiles;
    }

    /**
     * Overriding method to handle XML-element.
     * @param atts element attributes
     */
    public void startElement (final HashMap atts) {
        if ( isGetRevisionMode ) {
            if (!atts.isEmpty() &&
                atts.get("name") != null &&
                atts.get("kind") != null &&
                (atts.get("schedule") == null ||
                atts.get("schedule") != null &&
                !atts.get("schedule").equals("delete")) &&
                atts.get("committed-rev") != null) {
                for ( int loop = 0; loop < inputFiles.length; loop++ ) {
                    if ( inputFiles[loop].equals(".") ) {
                        if ( "dir".equals(atts.get("kind")) && ".".equals(atts.get("name")) ) {
                            entries.add(new Entry(".", atts.get("committed-rev").toString()));
                            break;
                        }
                    }
                    else if ( atts.get("name").equals(inputFiles[loop]) ) {
                        entries.add(new Entry(atts.get("name").toString(), atts.get("committed-rev").toString()));
                        break;
                    }
                }
            }
        }
        else {
            if (!atts.isEmpty() &&
                atts.get("name") != null &&
                atts.get("kind") != null &&
                atts.get("kind").equals("file") &&
                (atts.get("schedule") == null ||
                (atts.get("schedule") != null &&
                !atts.get("schedule").equals("delete"))) &&
                atts.get("committed-rev") != null) {
                    if ( atts.get("name").toString().toLowerCase().endsWith(".sql") || atts.get("name").toString().toLowerCase().endsWith(".pro") ) {
                        entries.add(new Entry(atts.get("name").toString(), atts.get("committed-rev").toString()));
                    }
            }
        }
    }
}
